package br.inf.solutions.apresentacoes.ssp.view.composer;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.springframework.context.annotation.Scope;
import org.zkoss.zk.ui.Component;

import br.com.vexillum.util.Return;
import br.inf.solutions.apresentacoes.ssp.control.hierarchy.HierarchyStructEntityController;
import br.inf.solutions.apresentacoes.ssp.model.Person;
import br.inf.solutions.apresentacoes.ssp.model.PostGraduation;
import br.inf.solutions.apresentacoes.ssp.model.Weapon;
import br.inf.solutions.apresentacoes.ssp.model.hierarchy.HierarchyStructEntity;
import br.inf.solutions.apresentacoes.ssp.view.composer.hierarchy.HierarchyStructEntityComposer;

@SuppressWarnings("serial")
@org.springframework.stereotype.Component
@Scope("prototype")
public class CommanderPanelComposer extends HierarchyStructEntityComposer{
	
	private List<Weapon> lstWeapon;
	private List<PostGraduation> lstPost;
	private List<Person> lstPersonPost;
	private List<Long> listaID;	
	private List<Person> listPerson;
	private HierarchyStructEntity unity;
	private PostGraduation selectedPost;
	private double media;
	private int max, min;
	private String faixa, squareName, postSquare;

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		if(parentComposer != null){
			setSquareName(((CommanderPanelComposer) parentComposer).getSquareName());
			setLstPersonPost(((CommanderPanelComposer) parentComposer).getLstPersonPost());
			setPostSquare(((CommanderPanelComposer) parentComposer).getPostSquare());			
		}
		if(lstPersonPost == null)
			lstPersonPost = new ArrayList<>();
		loadBinder();
	}
	
	public void recursivo(HierarchyStructEntity no){
		List<HierarchyStructEntity> filhos;
		listaID.add(no.getId());
		
		if(no.getChildrens() == null || no.getChildrens().size() <= 0){
			return;
		}
		
		filhos = no.getChildrens();
		for (HierarchyStructEntity HSE : filhos) {
			recursivo(HSE);
		}
	}
	
	@Override
	public Return showCadBox() {		
		unity = getTreeStructure().getSelectedItem().getValue();
		listaID = new ArrayList<Long>();		
		recursivo(unity);
	
		listPosts();
		listWeapons();
		listPersons();
	
		if(lstPost.size() > 0)
			getComponentById(component, "posto").setVisible(true);
		if(lstWeapon.size() > 0)
			getComponentById(component, "arma").setVisible(true);
		if(media != 0 && max != 0 && min != 0)
			getComponentById(component, "idade").setVisible(true);
				
		if(lstPost.size() == 0)
			getComponentById(component, "posto").setVisible(false);
		if(lstWeapon.size() == 0)
			getComponentById(component, "arma").setVisible(false);
		if(media == 0.0 && max == 0 && min == 0)
			getComponentById(component, "idade").setVisible(false);
		
		return new Return(true);
	}	
	
	@SuppressWarnings("unchecked")
	public Return listPosts(){
		Return ret = new Return(true);
		HierarchyStructEntityController control = getControl();
		ret.concat(control.doAction("getListPosts"));
		
		ArrayList<PostGraduation> pg = new ArrayList<PostGraduation>();
		ArrayList<Object> obj =  (ArrayList<Object>) ret.getList();
		
		for (Object object : obj) {			
			PostGraduation p = new PostGraduation();
			p.setNumberEffective((Long) ((Object[]) object)[0]);
			p.setName((String) ((Object[]) object)[1]);
			p.setId((Long) ((Object[]) object)[2]);			
			pg.add(p);
		}
			
		setLstPost(pg) ;
		loadBinder();
		return ret;
	}
	
	@SuppressWarnings("unchecked")
	public Return listWeapons(){
		Return ret = new Return(true);
		unity = getTreeStructure().getSelectedItem().getValue();
		setEntity(unity);
		HierarchyStructEntityController control = getControl();		
		ret.concat(control.doAction("getListWeapons"));		
		
		ArrayList<Weapon> w = new ArrayList<Weapon>();
		ArrayList<Object> obj =  (ArrayList<Object>) ret.getList();
				
		for (Object object : obj) {			
			Weapon we = new Weapon();
			we.setNumberEffective((Long) ((Object[]) object)[0]);
			we.getTypeWeapon().setName((String) ((Object[]) object)[1]);
			w.add(we);
		}		
		setLstWeapon(w);		
		loadBinder();
		return ret;
	}
	
	@SuppressWarnings("unchecked")
	public void listPersons(){
		Return ret = new Return(true);
		List<Person> lstPersons = new ArrayList<Person>();
		
		HierarchyStructEntityController control = getControl();
		ret.concat(control.doAction("getListPersons"));
		
		lstPersons.addAll((Collection<? extends Person>) ret.getList());
		
		if(lstPersons.size() > 0){
			media = 0; max = 0;
			min = 9999;
				
			for (Person p : lstPersons) {
				
				media  = p.getAge() + media;
							
				if(min > p.getAge() ){
					min = p.getAge();
				}
				if(max < p.getAge() ){
					max = p.getAge();
				}
				p.getAge();
			}
			media = ((media)/lstPersons.size());
			faixa = min + " a " + max +" anos";
			loadBinder();
		}else{
			media = 0.0; max= 0;
			min = 0; faixa = "";
			setListPerson(lstPersons);
			loadBinder();
		}		
	}
	
	@SuppressWarnings("unchecked")
	public Return retirement(){
		getSelectedPost();
		Return ret = new Return(true);
		
		HierarchyStructEntityController control = getControl();
		ret.concat(control.doAction("getRetirement"));
				
		this.setLstPersonPost((ArrayList<Person>) ret.getList());	
		setPostSquare(getSelectedPost().getName());
		callBookingTime();
		
		loadBinder();
		return ret;
	}
	
	public void callBookingTime(){
		callModalWindow("/paginas/painelcomandante/visualizarpessoaposto/temporeserva.zul");		
	}
	
	public String getSquareName() {
		return squareName;
	}

	public void setSquareName(String squareName) {
		this.squareName = squareName;
	}
	
	public List<Weapon> getLstWeapon() {
		return lstWeapon;
	}

	public void setLstWeapon(List<Weapon> lstWeapon) {
		this.lstWeapon = lstWeapon;
	}

	public List<PostGraduation> getLstPost() {
		return lstPost;
	}

	public void setLstPost(List<PostGraduation> lstPost) {
		this.lstPost = lstPost;
	}

	public List<Person> getLstPersonPost() {
		return lstPersonPost;
	}

	public void setLstPersonPost(List<Person> lstPersonPost) {
		this.lstPersonPost = lstPersonPost;
	}

	public List<Long> getListaID() {
		return listaID;
	}

	public void setListaID(List<Long> listaID) {
		this.listaID = listaID;
	}

	public double getMedia() {
		return media;
	}

	public void setMedia(double media) {
		this.media = media;
	}

	public int getMax() {
		return max;
	}

	public void setMax(int max) {
		this.max = max;
	}

	public int getMin() {
		return min;
	}

	public void setMin(int min) {
		this.min = min;
	}
	
	public String getFaixa() {
		return faixa;
	}

	public void setFaixa(String faixa) {
		this.faixa = faixa;
	}

	public PostGraduation getSelectedPost() {
		return selectedPost;
	}

	public void setSelectedPost(PostGraduation selectedPost) {
		this.selectedPost = selectedPost;
	}

	public List<Person> getListPerson() {
		return listPerson;
	}

	public void setListPerson(List<Person> listPerson) {
		this.listPerson = listPerson;
	}

	public String getPostSquare() {
		return postSquare;
	}

	public void setPostSquare(String postSquare) {
		this.postSquare = postSquare;
	}
}